package cn.txy.template;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.txy.config.WechatProperties;
import cn.txy.domain.OpenIdResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Random;

public class WechatLoginTemplate {
    private WechatProperties wechatProperties;

    public WechatLoginTemplate(WechatProperties wechatProperties) {
        this.wechatProperties = wechatProperties;
    }


    public OpenIdResult getOpenId(String code) {
        //得到请求地址
        String sendUrl = String.format(wechatProperties.getOpenidurl(), wechatProperties.getAppid(), wechatProperties.getAppsecret(), code);
        //发送请求
        HttpResponse execute = HttpRequest.get(sendUrl).execute();
        //得到返回结果
        String body = execute.body();
        //将结果转为对象
        OpenIdResult openIdResult = JSON.parseObject(body, OpenIdResult.class);
        String phone = getWechatPhone(code);
        openIdResult.setPhone(phone);
        return openIdResult;
    }

    public String getAccessToken() {
        //得到获得token的路径
        String access_token_url = String.format(wechatProperties.getAccesstokenurl(),  wechatProperties.getAppid(),  wechatProperties.getAppsecret());
        //执行发送请求
        HttpResponse execute = HttpRequest.get(access_token_url).execute();
        //得到结果
        String body = execute.body();
        //获取里面的token
        return JSON.parseObject(body).get("access_token").toString();
    }

    public String getWechatPhone(String code) {
        String accessToken = getAccessToken();
        //得到获得token的路径
        String access_token_url = String.format(wechatProperties.getPhonenumberurl(), accessToken);
        //执行发送请求
        HttpRequest post = HttpRequest.post(access_token_url);
        //携带请求参数
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",code);
        post.form(map);
        HttpResponse execute = post.execute();
        //得到结果
        String body = execute.body();
        //获取里面的token
         JSONObject jsonObject = JSON.parseObject(body);
        if (jsonObject.get("errmsg").toString().equals("ok")){
            return JSONObject.parseObject(jsonObject.get("phone_info")
                    .toString()).get("phoneNumber").toString();
        }
        return generateMobileNumber();
    }
    public static String generateMobileNumber() {
        // 手机号码的前三位，常见的号段
        String[] prefixes = {
                "130", "131", "132", "133", "134", "135", "136", "137", "138", "139",
                "145", "147",
                "150", "151", "152", "153", "155", "156", "157", "158", "159",
                "166",
                "170", "171", "172", "173", "175", "176", "177", "178",
                "180", "181", "182", "183", "184", "185", "186", "187", "188", "189",
                "198", "199"
        };

        Random random = new Random();
        // 随机选择一个号段
        String prefix = prefixes[random.nextInt(prefixes.length)];
        StringBuilder sb = new StringBuilder(prefix);

        // 随机生成后 8 位数字
        for (int i = 0; i < 8; i++) {
            sb.append(random.nextInt(10));
        }

        return sb.toString();
    }
}
